﻿using AjaxControlToolkit;
using ASP;
using ClassLibrary.BusinessLayer.Facade;
using IPMS.BLL;
using IPMS.Sort;
using Resources;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Globalization;
using System.Threading;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using Resources;
using Resources;
using AjaxControlToolkit;
using IPMS.AccessAuth;
using IPMS.Sort;
using System.Web;
using SysOperationLayer.DBHandle;
using SysComm;


public partial class ProjectGroupUpdate : Page, IRequiresSessionState
{
    LogDB m_logdb = new LogDB();
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        this.clearText();
        this.setTextReadOnly(true);
        this.setButtons(string.Empty);
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        try
        {
            Auth canView = new Auth(this.Session["UserId"].ToString(), string.Empty, "ProjectGroup");
            if (!canView.CanDelete())
            {
                base.Response.Redirect("ErrorPage/NotAllowed.aspx", false);
                return;
            }
        }
        catch (Exception ex)
        {
            base.Response.Redirect("Login.aspx");
        }
        try
        {
            this.ODSProjectGroups.DeleteMethod = "Delete";
            this.ODSProjectGroups.DeleteParameters["IsDelete"].DefaultValue = "true";
            this.ODSProjectGroups.DeleteParameters["DeleteBy"].DefaultValue = this.Page.User.Identity.Name.ToString();
            this.ODSProjectGroups.DeleteParameters["DeleteIP"].DefaultValue = HttpContext.Current.Request.UserHostAddress;
            this.ODSProjectGroups.DeleteParameters["Original_ProjectGroupID"].DefaultValue = this.txtProjectGroupID.Text.ToString();
            this.ODSProjectGroups.Delete();
            this.clearText();
            this.setTextReadOnly(true);
            this.setButtons(string.Empty);
            m_logdb.writeSucLog(SysUtil.m_functiong_projectgroup, "delete project group succeed!");
        }
        catch (Exception ex)
        {
            List<string> lstValues = new List<string>();
            //this.lblError.Text = IPDataFormat.GenerateMessage(ex, lstValues);
            this.lblError.Text = ex.Message.ToString();
            this.chkForOperation.Checked = true;
            m_logdb.writeFaileLog(SysUtil.m_functiong_projectgroup, "delete project group faile!");
        }
    }

    protected void btnModify_Click(object sender, EventArgs e)
    {
        try
        {
            Auth canView = new Auth(this.Session["UserId"].ToString(), string.Empty, "ProjectGroup");
            if (!canView.CanUpdate())
            {
                base.Response.Redirect("ErrorPage/NotAllowed.aspx", false);
                return;
            }
        }
        catch (Exception ex)
        {
            base.Response.Redirect("Login.aspx");
        }
        this.setTextReadOnly(false);
        this.setButtons("modify");
        this.txtProjectGroupID.ReadOnly = true;
        this.txtProjectGroupID.Enabled = false;
        this.txtTitleSubject.Focus();
    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        this.setTextReadOnly(false);
        this.clearText();
        this.setButtons("new");
        this.txtProjectGroupID.ReadOnly = false;
        this.txtProjectGroupID.Enabled = true;
        this.txtProjectGroupID.Focus();
    }

    protected void btnProjectClassSetBlank_Click(object sender, EventArgs e)
    {
        this.txtProjectClass.Text = string.Empty;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string trueUpdateStr = string.Empty;
            string falseUpdateStr = string.Empty;
            string trueInsertStr = string.Empty;
            string falseInsertStr = string.Empty;
            if (Session["Culture"] != null)
            {

                if (Session["Culture"].ToString() == "zh-TW")
                {
                    trueUpdateStr = "更新成功!";
                    falseUpdateStr = "更新失敗!";
                    trueInsertStr = "新增成功!";
                    falseInsertStr = "新增失敗!";
                }
                else if (Session["Culture"].ToString() == "zh-CN")
                {
                    trueUpdateStr = "更新成功!";
                    falseUpdateStr = "更新失败!";
                    trueInsertStr = "添加成功!";
                    falseInsertStr = "添加失败!";
                }
                else
                {
                    trueUpdateStr = "Update succeed!";
                    falseUpdateStr = "Update failed!";
                    trueInsertStr = "Add succeed!";
                    falseUpdateStr = "Add failed!";
                }
            }
            if (!this.chkForUpdate.Checked)
            {
                this.ODSProjectGroups.InsertMethod = "Insert";
                this.ODSProjectGroups.InsertParameters["ProjectGroupID"].DefaultValue = this.txtProjectGroupID.Text.ToString();
                this.ODSProjectGroups.InsertParameters["Description"].DefaultValue = this.txtTitleSubject.Text.ToString();
                this.ODSProjectGroups.InsertParameters["OfficerName"].DefaultValue = this.txtOfficerName.Text.ToString();
                this.ODSProjectGroups.InsertParameters["OfficerRole"].DefaultValue = this.txtOfficerRole.Text.ToString();
                this.ODSProjectGroups.InsertParameters["Title"].DefaultValue = this.txtTitle.Text.ToString();
                this.ODSProjectGroups.InsertParameters["OfficerEmail"].DefaultValue = this.txtOfficerEmail.Text.ToString();
                this.ODSProjectGroups.InsertParameters["CreateBy"].DefaultValue = this.Page.User.Identity.Name.ToString();
                this.ODSProjectGroups.InsertParameters["UpdateBy"].DefaultValue = this.Page.User.Identity.Name.ToString();
                this.ODSProjectGroups.InsertParameters["OwnerGroupID"].DefaultValue = this.Session["OwnerGroupID"].ToString();
                this.ODSProjectGroups.InsertParameters["FirstFilingDate"].DefaultValue = string.Empty;
                this.ODSProjectGroups.InsertParameters["PriorityDate"].DefaultValue = string.Empty;
                this.ODSProjectGroups.InsertParameters["ProjectClass"].DefaultValue = this.txtProjectClass.Text.ToString();
                this.ODSProjectGroups.InsertParameters["Applicant"].DefaultValue = this.txtApplicant.Text.ToString();
                this.ODSProjectGroups.InsertParameters["Inventor"].DefaultValue = this.txtInventor.Text.ToString();
                this.ODSProjectGroups.InsertParameters["Type"].DefaultValue = this.ddlType.SelectedValue.ToString();
                this.ODSProjectGroups.InsertParameters["Client"].DefaultValue = this.txtClient.Text;
                this.ODSProjectGroups.InsertParameters["TitleSubject"].DefaultValue = this.txtTitleSubject.Text.ToString();
                this.ODSProjectGroups.InsertParameters["CreateIP"].DefaultValue = HttpContext.Current.Request.UserHostAddress;

                if (this.ODSProjectGroups.Insert() < 0)
                {
                    IPMS.Common.Jscript.AlertAndRedirect(trueInsertStr, "ProjectGroupSearch.aspx", this.Page);
                    m_logdb.writeSucLog(SysUtil.m_functiong_projectgroup, "add project group succeed!");
                }
                else
                {
                    IPMS.Common.Jscript.Alert(falseInsertStr, this.Page);
                    m_logdb.writeFaileLog(SysUtil.m_functiong_projectgroup, "add project group faile!");
                }
            }
            else
            {
                this.ODSProjectGroups.UpdateMethod = "Update";
                this.ODSProjectGroups.UpdateParameters["ProjectGroupID"].DefaultValue = this.txtProjectGroupID.Text.ToString();
                this.ODSProjectGroups.UpdateParameters["Description"].DefaultValue = this.txtTitleSubject.Text.ToString();
                this.ODSProjectGroups.UpdateParameters["OfficerName"].DefaultValue = this.txtOfficerName.Text.ToString();
                this.ODSProjectGroups.UpdateParameters["OfficerRole"].DefaultValue = this.txtOfficerRole.Text.ToString();
                this.ODSProjectGroups.UpdateParameters["Title"].DefaultValue = this.txtTitle.Text.ToString();
                this.ODSProjectGroups.UpdateParameters["OfficerEmail"].DefaultValue = this.txtOfficerEmail.Text.ToString();
                this.ODSProjectGroups.UpdateParameters["UpdateBy"].DefaultValue = this.Page.User.Identity.Name.ToString();
                this.ODSProjectGroups.UpdateParameters["FirstFilingDate"].DefaultValue = string.Empty;
                this.ODSProjectGroups.UpdateParameters["PriorityDate"].DefaultValue = string.Empty;
                this.ODSProjectGroups.UpdateParameters["ProjectClass"].DefaultValue = this.txtProjectClass.Text.ToString();
                this.ODSProjectGroups.UpdateParameters["Applicant"].DefaultValue = this.txtApplicant.Text.ToString();
                this.ODSProjectGroups.UpdateParameters["Inventor"].DefaultValue = this.txtInventor.Text.ToString();
                this.ODSProjectGroups.UpdateParameters["Type"].DefaultValue = this.ddlType.SelectedValue.ToString();
                this.ODSProjectGroups.UpdateParameters["Client"].DefaultValue = this.txtClient.Text;
                this.ODSProjectGroups.UpdateParameters["Original_ProjectGroupID"].DefaultValue = this.txtProjectGroupID.Text.ToString();
                this.ODSProjectGroups.UpdateParameters["TitleSubject"].DefaultValue = this.txtTitleSubject.Text.ToString();
                this.ODSProjectGroups.UpdateParameters["UpdateIP"].DefaultValue = HttpContext.Current.Request.UserHostAddress;
                if (this.ODSProjectGroups.Update() < 0)
                {
                    IPMS.Common.Jscript.AlertAndRedirect(trueUpdateStr, "ProjectGroupSearch.aspx", this.Page);
                    m_logdb.writeSucLog(SysUtil.m_functiong_projectgroup, "update project group information succeed!");
                }
                else
                {
                    IPMS.Common.Jscript.Alert(falseUpdateStr, this.Page);
                    m_logdb.writeFaileLog(SysUtil.m_functiong_projectgroup, "update project group information faile!");
                }
            }
            this.setTextReadOnly(true);
            this.setButtons("save");
            this.chkForUpdate.Checked = false;
            this.clearText();
            this.Search();
        }
        catch (Exception ex)
        {
            List<string> lstValues = new List<string>();
            //this.lblError.Text = IPDataFormat.GenerateMessage(ex, lstValues);
            this.lblError.Text = ex.Message.ToString();
            if (this.chkForUpdate.Checked)
            {
                this.setButtons("modify");
            }
            else
            {
                this.setButtons("new");
            }
            this.chkForOperation.Checked = true;
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if ((this.Session["SelectedClient"] != null) && (this.Session["SelectedClient"].ToString() != ""))
        {
            this.txtClient.Text = this.Session["SelectedClient"].ToString();
            ClientsFacade facade = new ClientsFacade();
            SqlParameter[] sqlparam = new SqlParameter[4];
            sqlparam[0] = new SqlParameter();
            sqlparam[0].ParameterName = "@Client";
            sqlparam[0].Value = this.txtClient.Text.ToString();
            sqlparam[1] = new SqlParameter();
            sqlparam[1].ParameterName = "@Name";
            sqlparam[1].Value = "";
            sqlparam[2] = new SqlParameter();
            sqlparam[2].ParameterName = "@ChineseName";
            sqlparam[2].Value = "";
            sqlparam[3] = new SqlParameter();
            sqlparam[3].ParameterName = "@ContactPerson";
            sqlparam[3].Value = "";
            DbParameter[] param = sqlparam;
            DataTable dt = facade.GetsClients("_IPMS_ClientSelect_SP", param);
            this.txtClientName.Text = dt.Rows[0]["Name"].ToString();
        }
    }

    protected void btnSearchProject_Click(object sender, EventArgs e)
    {
        this.Search();
    }

    protected void clearText()
    {
        this.txtProjectGroupID.Text = string.Empty;
        this.txtTitleSubject.Text = string.Empty;
        this.txtOfficerName.Text = string.Empty;
        this.txtOfficerRole.Text = string.Empty;
        this.txtTitle.Text = string.Empty;
        this.txtOfficerEmail.Text = string.Empty;
        this.txtProjectClass.Text = string.Empty;
        this.txtApplicant.Text = string.Empty;
        this.txtInventor.Text = string.Empty;
        this.txtTitleSubject.Text = string.Empty;
        this.chkForUpdate.Checked = false;
        this.chkForOperation.Checked = false;
        this.txtClient.Text = string.Empty;
        this.lblError.Text = string.Empty;
        this.txtClientName.Text = string.Empty;
        this.ddlType.SelectedIndex = -1;
    }

    protected void ddlType_PreRender(object sender, EventArgs e)
    {
        if (!this.Page.IsPostBack)
        {
            try
            {
                if (this.ddlType.Items.FindByText("").Text != "")
                {
                }
            }
            catch (NullReferenceException)
            {
                this.ddlType.Items.Add(new ListItem("", "0"));
            }
            IPSortList.SortByValue(this.ddlType);
            this.ddlType.SelectedIndex = -1;
            this.ddlType.Items.FindByValue("").Selected = true;
        }
    }

    protected void gvChooseProjectClass_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.txtProjectClass.Text = this.gvChooseProjectClass.DataKeys[this.gvChooseProjectClass.SelectedIndex].Values["ProjectClass"].ToString();
    }

    protected void gvProjectGrp_PageIndexChanging1(object sender, GridViewPageEventArgs e)
    {
        this.gvProjectGrp.PageIndex = e.NewPageIndex;
        this.gvProjectGrp.DataBind();
        this.Search();
    }

    protected void gvProjectGrp_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.chkForUpdate.Checked = true;
        this.chkForOperation.Checked = true;
        ProjectGroupClass runMethod = new ProjectGroupClass();
        runMethod.GetProjectGroupInfo(this.gvProjectGrp.DataKeys[this.gvProjectGrp.SelectedIndex].Values["ProjectGroupID"].ToString());
        this.txtProjectGroupID.Text = this.gvProjectGrp.DataKeys[this.gvProjectGrp.SelectedIndex].Values["ProjectGroupID"].ToString();
        this.txtTitleSubject.Text = runMethod.Description;
        this.txtOfficerName.Text = runMethod.OfficerName;
        this.txtOfficerRole.Text = runMethod.OfficerRole;
        this.txtTitle.Text = runMethod.Title;
        this.txtOfficerEmail.Text = runMethod.OfficerEmail;
        this.txtProjectClass.Text = runMethod.ProjectClass;
        this.txtApplicant.Text = runMethod.Applicant;
        this.txtInventor.Text = runMethod.Inventor;
        this.txtClient.Text = runMethod.Client;
        this.txtClientName.Text = runMethod.ClientName;
        this.ddlType.SelectedIndex = -1;
        this.ddlType.Items.FindByText(runMethod.Type.ToString()).Selected = true;
        this.setButtons(string.Empty);
    }

    protected override void InitializeCulture()
    {
        string sCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();
        try
        {
            sCulture = this.Session["Culture"].ToString();
        }
        catch (NullReferenceException)
        {
        }
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(sCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(sCulture);
        base.InitializeCulture();
    }

    protected void OKButton_Click(object sender, EventArgs e)
    {
        if (this.gvChooseProjectClass.SelectedIndex >= 0)
        {
            this.txtProjectClass.Text = this.gvChooseProjectClass.DataKeys[this.gvChooseProjectClass.SelectedIndex].Values["ProjectClass"].ToString();
        }
    }

    private void FillDDl()
    {
        IPTableAdapters.CodeTableAdapter ct = new IPTableAdapters.CodeTableAdapter();
        IP.CodeDataTable dt = ct.GetCodesByType("PT");
        ddlType.DataSource = dt;
        ddlType.DataTextField = "Code";
        ddlType.DataValueField = "Code";
        ddlType.DataBind();
        ddlType.Items.Insert(0, new ListItem("", ""));
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            Auth canView = new Auth(this.Session["UserId"].ToString(), string.Empty, "ProjectGroup");
            if (!canView.CanUpdate())
            {
                base.Response.Redirect("ErrorPage/NotAllowed.aspx", false);
                return;
            }
        }
        catch (Exception ex)
        {
            base.Response.Redirect("Login.aspx");
        }
        if (!this.Page.IsPostBack)
        {
            ((Label)this.Master.FindControl("lblMstTitle")).Text = "| " + ISitemap.ProjectGroupManage.ToString() + " | " + ISitemap.ProjectGroupUpdate.ToString();

            FillDDl();
            this.setTextReadOnly(true);
            this.setButtons(string.Empty);
            this.btnDelete.Attributes.Add("onClick", "return confirm('" + Messages.DeleteConfirmation.ToString() + "')");

            if (Request["ProjectGroupID"] != null)
            {
                this.chkForUpdate.Checked = true;
                this.chkForOperation.Checked = true;
                ProjectGroupClass runMethod = new ProjectGroupClass();
                runMethod.GetProjectGroupInfo(Request["ProjectGroupID"]);
                this.txtProjectGroupID.Text = Request["ProjectGroupID"];
                this.txtTitleSubject.Text = runMethod.Description;
                this.txtOfficerName.Text = runMethod.OfficerName;
                this.txtOfficerRole.Text = runMethod.OfficerRole;
                this.txtTitle.Text = runMethod.Title;
                this.txtOfficerEmail.Text = runMethod.OfficerEmail;
                this.txtProjectClass.Text = runMethod.ProjectClass;
                this.txtApplicant.Text = runMethod.Applicant;
                this.txtInventor.Text = runMethod.Inventor;
                this.txtClient.Text = runMethod.Client;
                this.txtClientName.Text = runMethod.ClientName;
                this.ddlType.SelectedIndex = -1;
                this.ddlType.Items.FindByValue(runMethod.Type.ToString()).Selected = true;

                this.setButtons(string.Empty);
            }

            this.setTextReadOnly(false);
            this.txtProjectGroupID.ReadOnly = true;
            this.txtProjectGroupID.Enabled = false;
            this.txtTitleSubject.Focus();
        }
    }

    protected void Search()
    {
        ProjectGroupFacade facade = new ProjectGroupFacade();
        SqlParameter[] sqlparam = new SqlParameter[2];
        sqlparam[0] = new SqlParameter();
        sqlparam[0].ParameterName = "@ProjectGroupID";
        sqlparam[0].Value = this.txtSearchProjectGroup.Text.ToString();
        sqlparam[1] = new SqlParameter();
        sqlparam[1].ParameterName = "@Description";
        sqlparam[1].Value = this.txtSearchTitleSubject.Text.ToString();
        DbParameter[] param = sqlparam;
        DataView dv = facade.GetsProjectGroup("_IPMS_ProjectGroupSearch_Sp", param).DefaultView;
        this.gvProjectGrp.DataSourceID = string.Empty;
        this.gvProjectGrp.DataSource = dv;
        this.gvProjectGrp.DataBind();
    }

    protected void setButtons(string action)
    {
        switch (action)
        {
            //case "new":
            //    this.btnNew.Enabled = false;
            //    this.btnModify.Enabled = false;
            //    this.btnSave.Enabled = true;
            //    this.btnDelete.Enabled = false;
            //    return;

            //case "modify":
            //    this.btnNew.Enabled = false;
            //    this.btnModify.Enabled = false;
            //    this.btnSave.Enabled = true;
            //    this.btnDelete.Enabled = false;
            //    return;

            case "save":
                //this.btnNew.Enabled = true;
               // this.btnModify.Enabled = false;
                this.btnSave.Enabled = true;
                this.btnDelete.Enabled = false;
                return;
        }
       // this.btnNew.Enabled = true;
       // this.btnModify.Enabled = this.txtProjectGroupID.Text.Length != 0;
        this.btnSave.Enabled = true;
        this.btnDelete.Enabled = this.txtProjectGroupID.Text.Length != 0;
    }

    protected void setTextReadOnly(bool bbool)
    {
        this.txtProjectGroupID.ReadOnly = bbool;
        this.txtTitleSubject.ReadOnly = bbool;
        this.txtOfficerName.ReadOnly = bbool;
        this.txtOfficerRole.ReadOnly = bbool;
        this.txtTitle.ReadOnly = bbool;
        this.txtTitleSubject.ReadOnly = bbool;
        this.txtOfficerEmail.ReadOnly = bbool;
        this.txtProjectClass.ReadOnly = bbool;
        this.btnProjectClass.Enabled = !bbool;
        this.txtApplicant.ReadOnly = bbool;
        this.txtInventor.ReadOnly = bbool;
        this.ddlType.Enabled = !bbool;
        this.txtClient.Enabled = !bbool;
    }
}
